/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2025-06-04     31603       the first version
 */

#include <rtdbg.h>
#include <rtthread.h>
#include <rtdef.h>
#include <drivers/pin.h>
#include <rtdevice.h>

#include "tim.h"


/* 定义线程控制块变量 begin */
#define                            PWM_THREAD_STACK_SIZE    1024
static  rt_uint8_t      PWM_stack[PWM_THREAD_STACK_SIZE];
struct  rt_thread       PWM_thread;


#define PWM_DEV_NAME        "pwm3"  /* PWM 设备名称 */
struct rt_device_pwm *pwm_dev;      /* PWM 设备句柄 */
#define PWM_DEV_CHANNEL1     1
#define PWM_DEV_CHANNEL2     2
#define PWM_DEV_CHANNEL3     3
#define PWM_DEV_CHANNEL4     4
rt_uint32_t period = 20000000;


int PWM_dev_init(void){

    extern void MX_TIM3_Init(void);
    MX_TIM3_Init();
    __HAL_TIM_SET_COMPARE(&htim3, PWM_DEV_CHANNEL1, 1000);
    HAL_TIM_PWM_Start(&htim3 ,PWM_DEV_CHANNEL1);
    __HAL_TIM_SET_COMPARE(&htim3, PWM_DEV_CHANNEL2, 1000);
    HAL_TIM_PWM_Start(&htim3 ,PWM_DEV_CHANNEL2);
    __HAL_TIM_SET_COMPARE(&htim3, PWM_DEV_CHANNEL3, 1000);
    HAL_TIM_PWM_Start(&htim3 ,PWM_DEV_CHANNEL3);
    __HAL_TIM_SET_COMPARE(&htim3, PWM_DEV_CHANNEL4, 1000);
    HAL_TIM_PWM_Start(&htim3 ,PWM_DEV_CHANNEL4);

    /* 查找设备 */
    pwm_dev = (struct rt_device_pwm *)rt_device_find(PWM_DEV_NAME);

    if(pwm_dev == RT_NULL){
        rt_kprintf("no find pwm dev");
        return RT_ERROR;
    }

    rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL1, 20000000, 500000);
    rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL2, 20000000, 500000);
    rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL3, 20000000, 500000);
    rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL4, 20000000, 500000);

    rt_pwm_enable(pwm_dev, PWM_DEV_CHANNEL1);
    rt_pwm_enable(pwm_dev, PWM_DEV_CHANNEL2);
    rt_pwm_enable(pwm_dev, PWM_DEV_CHANNEL3);
    rt_pwm_enable(pwm_dev, PWM_DEV_CHANNEL4);


    rt_kprintf("pwm dev ok");
    return RT_EOK;
}
void pwm1_set_angle(int angle){
    if (angle < 0) angle = 0;
    if (angle > 180) angle = 180;


    rt_uint32_t pulse_width = 500000 + (angle * 2000000 ) / 180;

    rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL1, period, pulse_width);

}
void pwm2_set_angle(int angle){
    if (angle < 0) angle = 0;
    if (angle > 180) angle = 180;


    rt_uint32_t pulse_width = 500000 + (angle * 2000000 ) / 180;

    rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL2, period, pulse_width);

}
void pwm3_set_angle(int angle){
//    if (angle < 0) angle = 0;
//    if (angle > 180) angle = 180;
//
//
//    rt_uint32_t pulse_width = 500000 + (angle * 2000000 ) / 180;
//
//    rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL3, period, pulse_width);
    int complementary_angle = 180 - angle;

    if (complementary_angle < 0) complementary_angle = 0;
    if (complementary_angle > 180) complementary_angle = 180;

    rt_uint32_t pulse_width = 500000 + (complementary_angle * 2000000 ) / 180;

    rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL3, period, pulse_width);

}
void pwm4_set_angle(int angle){
    if (angle < 0) angle = 0;
    if (angle > 180) angle = 180;


    rt_uint32_t pulse_width = 500000 + (angle * 2000000 ) / 180;

    rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL4, period, pulse_width);

}





